﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using IfSoft.OA.Entities;
using IfSoft.OA.DataAccess;

namespace IfSoft.OA.Business
{
    public class UserManager : IUserManager
    {
        #region Private Members

        private IUserRepository _repository;

        #endregion

        #region Constrcutor

        public UserManager()
            : this(null)
        {
        }

        public UserManager(IUserRepository repository)
        {
            if (repository == null)
                _repository = new UserRepository();
            else
                _repository = repository;
        }

        #endregion

        #region Basic CRUD operations

        public void Create(User bo)
        {
            _repository.Create(bo);
        }

        public void Update(User bo)
        {
            _repository.Update(bo);
        }

        public void Delete(int id)
        {
            _repository.Delete(id);
        }

        public User SearchById(int id)
        {
            return _repository.SearchById(id);
        }

        public List<User> Search(UserCriteria criteria)
        {
            return _repository.Search(criteria);
        }

        public PagedList<User> SearchPagedList(UserCriteria criteria)
        {
            return _repository.SearchPagedList(criteria);
        }

        #endregion

        public User SearchByUserName(string userName)
        {
            return _repository.SearchByUserName(userName);
        }

        public bool IsExist(string userName)
        {
            User user = _repository.SearchByUserName(userName);
            if (user != null && user.Id>0)
                return true;
            return false;
        }

        public bool ValidateUser(string userName, string password)
        {
            User user = _repository.SearchByUserName(userName);
            return (user != null) && (user.Password == password);          
        }

        //public string[] GetRoles(int id)
        //{
        //    List<Role> roles = _repository.GetRoles(id);
        //    List<string> list = roles.Select(t=>t.Code).ToList();
        //    return list.ToArray();       
        //}
    }
}